package com.zxp.drools.controller;

import com.drools.core.KieTemplate;
import com.zxp.drools.bean.Address;
import com.zxp.drools.bean.Order;
import com.zxp.drools.bean.fact.AddressCheckResult;
import com.zxp.drools.service.ReloadDroolsRulesService;
import org.drools.compiler.shade.org.eclipse.jdt.internal.compiler.batch.ClasspathLocation;
import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.Collection;

/**
 * @author zxp
 * @date 2021-12-21 16:28
 */
@RestController
public class OrderDroolsController {

    @Autowired
    private KieTemplate kieTemplate;

    @Autowired
    private ReloadDroolsRulesService ruleService;

    @Autowired
    private KieBase kieBase;

    @GetMapping("/saveOrder")
    public Order saveOrder(Order order) throws Exception {

        File directory = new File("");//参数为空
        String courseFile = directory.getCanonicalPath(); //标准的路径 ;
        String author =directory.getAbsolutePath(); //绝对路径;

        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        Resource resource = resourcePatternResolver.getResource("classpath:/rules/order.drl");
        String path = resource.getFile().getAbsolutePath();


        String  property = System.getProperty("user.dir");
        KieSession kieSession = kieTemplate.getKieSession("order.drl","attribute-rule.drl");

        KieSession kieSession2 = kieBase.newKieSession();
        kieSession.insert(order);

        kieSession.fireAllRules();

        return order;
    }



    @ResponseBody
    @RequestMapping("/address")
    public void test(int num){
        Address address = new Address();
        address.setPostcode(generateRandom(num));

        ruleService.reload();
        KieSession kieSession = ReloadDroolsRulesService.kieContainer.newKieSession();

        AddressCheckResult result = new AddressCheckResult();
        kieSession.insert(address);
        kieSession.insert(result);
        int ruleFiredCount = kieSession.fireAllRules();
        kieSession.destroy();
        System.out.println("触发了" + ruleFiredCount + "条规则");

        if(result.isPostCodeResult()){
            System.out.println("规则校验通过");
        }

    }

    /**
     * 从数据加载最新规则
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/reload")
    public String reload() throws IOException {
        ruleService.reload();
        return "ok";
    }


    /**
     * 生成随机数
     * @param num
     * @return
     */
    public String generateRandom(int num) {
        String chars = "0123456789";
        StringBuffer number=new StringBuffer();
        for (int i = 0; i < num; i++) {
            int rand = (int) (Math.random() * 10);
            number=number.append(chars.charAt(rand));
        }
        return number.toString();
    }

}
